package com.jr.service.impl;

import com.jr.mapper.StationMapper;
import com.jr.pojo.BookingStation;
import com.jr.pojo.Station;
import com.jr.service.IBookingStation;
import com.jr.uitil.PageHelper;
import com.jr.vo.MyBooking;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;

@Service
public class BookingStationImpl implements IBookingStation {

    @Autowired
    private StationMapper stationMapper;

    @Override
    public ArrayList<Station> queryStation(String session) {
        return stationMapper.selectStation(session);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean addBookingStation(BookingStation b) {
        BookingStation selectbooked = stationMapper.selectbooked(b);
        if (selectbooked != null) {
            BookingStation bs = stationMapper.selectBid(b);
            if (bs != null) {
                System.out.println("当前工位已经被预约");
                return false;
            }

            int inserbs = stationMapper.updatebooked(b);
            if (inserbs <= 0) {
                throw new RuntimeException("该工位在添加失败");
            }

            MyBooking mb = new MyBooking();
            mb.setStatus(1); // 明确设置为“已预约”状态
            mb.setUserid(b.getUserid());
            mb.setTimestamps(b.getTimestamps());
            mb.setStid(b.getStid());
            mb.setChoosetime(b.getChoosetime());
            mb.setChoosedate(b.getChoosedate());

            int insertStatus = stationMapper.updatemybooked(mb);
            if (insertStatus <= 0) {
                throw new RuntimeException("添加预约工位失败");
            }
            return true;
        } else {
            // 第一次预约的逻辑不变
            int inserbs = stationMapper.insertBookingStation(b);
            if (inserbs <= 0) {
                throw new RuntimeException("该工位在添加失败已被预订");
            }
            MyBooking mb = new MyBooking();
            mb.setStatus(1);
            mb.setUserid(b.getUserid());
            mb.setTimestamps(b.getTimestamps());
            int insertStatus = stationMapper.insertInid(mb);
            if (insertStatus <= 0) {
                throw new RuntimeException("添加预约工位失败");
            }
            return true;
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public ArrayList<MyBooking> queryMyBooking(MyBooking mb, Integer pageindex, Integer shownum) {
        return stationMapper.selectMyBooking(mb, pageindex, shownum);
    }

    @Override
    public BookingStation queryDuplicateBooking(BookingStation b) {
        return stationMapper.selectDuplicateBooking(b);
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updateMyBookingStatus(BookingStation b) {
        return stationMapper.updateMyBookingStatus(b)>0;
    }


}
